package com.xyoto.takeawayUser.service.impl;

import com.xyoto.takeawayUser.enums.ShoppingEnum;
import com.xyoto.takeawayUser.exception.ExceptionUtils;
import com.xyoto.takeawayUser.mapper.HomeMapper;
import com.xyoto.takeawayUser.po.ShoppingPO;
import com.xyoto.takeawayUser.service.HomeService;
import com.xyoto.takeawayUser.utils.Result;
import com.xyoto.takeawayUser.vo.ShoppingVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class HomeServiceImpl implements HomeService {

    @Autowired
    private HomeMapper homeMapper;

    /**
     * 通过商品id获取商品详情
     * @param shoppingId
     * @return
     */
    @Override
    public Result getShoppingInfo(Integer shoppingId) {
        log.info("查询商品信息开始");
        if (shoppingId == null || shoppingId <= 0){
            log.info("前端传过来的商品id错误");
            ExceptionUtils.error(ShoppingEnum.SHOPPING_ID_IS_NULL);
        }

        ShoppingPO shoppingPO = homeMapper.getShoppingInfo(shoppingId);
        if (shoppingPO == null){
            log.info("前端传过来的商品id查不到详情");
            ExceptionUtils.error(ShoppingEnum.SHOPPING_INFO_IS_NULL);
        }

        ShoppingVO shoppingVO = new ShoppingVO();
        BeanUtils.copyProperties(shoppingPO, shoppingVO);

        log.info("查询商品详情成功");
        return Result.success(shoppingVO);
    }
}
